package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;

import app.Board;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * 
 * @author 116@lab0.net
 * 
 *         The main GUI window.
 * 
 */
public class HungarianRingsGui
{
    private Board                   board = new Board();
    private JFrame                  frmHungarianRings;
    private HungarianRingsComponent hungarianRingsComponent;
    private JPanel                  panel;
    private JButton                 btnTurn;
    private JButton                 btnTurn_1;
    private JButton                 btnShuffle;
    private JButton                 btnRturn;
    private JButton                 btnRturn2;
    private JButton btnKeys;
    
    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                try
                {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    HungarianRingsGui window = new HungarianRingsGui();
                    window.frmHungarianRings.setVisible(true);
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the application.
     */
    public HungarianRingsGui()
    {
        initialize();
    }
    
    /**
     * Initialize the contents of the frame.
     */
    private void initialize()
    {
        frmHungarianRings = new JFrame();
        frmHungarianRings.setTitle("Hungarian rings");
        frmHungarianRings.setBounds(100, 100, 450, 300);
        frmHungarianRings.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmHungarianRings.getContentPane().setLayout(new BorderLayout(0, 0));
        frmHungarianRings.getContentPane().add(getHungarianRingsComponent(), BorderLayout.CENTER);
        frmHungarianRings.getContentPane().add(getPanel(), BorderLayout.WEST);
        frmHungarianRings.setExtendedState(Frame.MAXIMIZED_BOTH);
    }
    
    private HungarianRingsComponent getHungarianRingsComponent()
    {
        if (hungarianRingsComponent == null)
        {
            hungarianRingsComponent = new HungarianRingsComponent(board);
        }
        return hungarianRingsComponent;
    }
    
    private JPanel getPanel()
    {
        if (panel == null)
        {
            panel = new JPanel();
            GridBagLayout gbl_panel = new GridBagLayout();
            gbl_panel.columnWidths = new int[] { 0, 0 };
            gbl_panel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0 };
            gbl_panel.columnWeights = new double[] { 0.0, Double.MIN_VALUE };
            gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
            panel.setLayout(gbl_panel);
            GridBagConstraints gbc_btnTurn = new GridBagConstraints();
            gbc_btnTurn.fill = GridBagConstraints.HORIZONTAL;
            gbc_btnTurn.insets = new Insets(0, 0, 5, 0);
            gbc_btnTurn.gridx = 0;
            gbc_btnTurn.gridy = 0;
            panel.add(getBtnTurn(), gbc_btnTurn);
            GridBagConstraints gbc_btnRturn = new GridBagConstraints();
            gbc_btnRturn.fill = GridBagConstraints.HORIZONTAL;
            gbc_btnRturn.insets = new Insets(0, 0, 5, 0);
            gbc_btnRturn.gridx = 0;
            gbc_btnRturn.gridy = 1;
            panel.add(getBtnRturn(), gbc_btnRturn);
            GridBagConstraints gbc_btnTurn_1 = new GridBagConstraints();
            gbc_btnTurn_1.fill = GridBagConstraints.HORIZONTAL;
            gbc_btnTurn_1.insets = new Insets(0, 0, 5, 0);
            gbc_btnTurn_1.gridx = 0;
            gbc_btnTurn_1.gridy = 2;
            panel.add(getBtnTurn_1(), gbc_btnTurn_1);
            GridBagConstraints gbc_btnRturn2 = new GridBagConstraints();
            gbc_btnRturn2.fill = GridBagConstraints.HORIZONTAL;
            gbc_btnRturn2.insets = new Insets(0, 0, 5, 0);
            gbc_btnRturn2.gridx = 0;
            gbc_btnRturn2.gridy = 3;
            panel.add(getBtnRturn2(), gbc_btnRturn2);
            GridBagConstraints gbc_btnShuffle = new GridBagConstraints();
            gbc_btnShuffle.insets = new Insets(0, 0, 5, 0);
            gbc_btnShuffle.fill = GridBagConstraints.HORIZONTAL;
            gbc_btnShuffle.gridx = 0;
            gbc_btnShuffle.gridy = 4;
            panel.add(getBtnShuffle(), gbc_btnShuffle);
            GridBagConstraints gbc_btnKeys = new GridBagConstraints();
            gbc_btnKeys.fill = GridBagConstraints.HORIZONTAL;
            gbc_btnKeys.gridx = 0;
            gbc_btnKeys.gridy = 5;
            panel.add(getBtnKeys(), gbc_btnKeys);
        }
        return panel;
    }
    
    private void turn1()
    {
        board.turn1();
        hungarianRingsComponent.repaint();
    }
    
    private void turn2()
    {
        board.turn2();
        hungarianRingsComponent.repaint();
    }
    
    private void shuffle()
    {
        board.shuffle();
        hungarianRingsComponent.repaint();
    }
    
    private void reverseTurn1()
    {
        board.reverseTurn1();
        hungarianRingsComponent.repaint();
    }
    
    private void reverseTurn2()
    {
        board.reverseTurn2();
        hungarianRingsComponent.repaint();
    }
    
    private JButton getBtnTurn()
    {
        if (btnTurn == null)
        {
            btnTurn = new JButton("Turn 1");
            btnTurn.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    turn1();
                }
            });
        }
        return btnTurn;
    }
    
    private JButton getBtnTurn_1()
    {
        if (btnTurn_1 == null)
        {
            btnTurn_1 = new JButton("Turn 2");
            btnTurn_1.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    turn2();
                }
            });
        }
        return btnTurn_1;
    }
    
    private JButton getBtnShuffle()
    {
        if (btnShuffle == null)
        {
            btnShuffle = new JButton("Shuffle");
            btnShuffle.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    shuffle();
                }
            });
        }
        return btnShuffle;
    }
    
    private JButton getBtnRturn()
    {
        if (btnRturn == null)
        {
            btnRturn = new JButton("RTurn 1");
            btnRturn.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    reverseTurn1();
                }
            });
        }
        return btnRturn;
    }
    
    private JButton getBtnRturn2()
    {
        if (btnRturn2 == null)
        {
            btnRturn2 = new JButton("RTurn2");
            btnRturn2.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {
                    reverseTurn2();
                }
            });
        }
        return btnRturn2;
    }
    private JButton getBtnKeys() {
        if (btnKeys == null) {
        	btnKeys = new JButton("Keys");
        	btnKeys.addKeyListener(new KeyAdapter()
            {
                @Override
                public void keyPressed(KeyEvent e)
                {
                    switch (e.getKeyCode())
                    {
                        case KeyEvent.VK_UP:
                            reverseTurn2();
                            break;

                        case KeyEvent.VK_DOWN:
                            turn2();
                            break;
                            
                        case KeyEvent.VK_LEFT:
                            reverseTurn1();
                            break;
                            
                        case KeyEvent.VK_RIGHT:
                            turn1();
                            break;
                        
                        default:
                            System.out.println(e.getKeyChar());
                            break;
                    }
                }
            });
        }
        return btnKeys;
    }
}
